using System;
using System.Text;

namespace SharpObjects.Cryptography
{

	public class Cryptographer
	{
		protected ICryptoProvider m_provider = null;

		public Cryptographer(ICryptoProvider provider)
		{
			m_provider = provider;
		}

		public byte [] Encrypt(string sUtf8Text)
		{
			return m_provider.Encrypt(Encoding.UTF8.GetBytes(sUtf8Text));
		}

		public byte [] Encrypt(string sText, System.Text.Encoding encoding)
		{
			return m_provider.Encrypt(encoding.GetBytes(sText));
		}

		public byte [] Encrypt(byte [] data)
		{
			return m_provider.Encrypt(data);
		}

		public string EncryptToBase64Text(string sUtf8Text)
		{
			byte [] encrypted = m_provider.Encrypt(Encoding.UTF8.GetBytes(sUtf8Text));
			return Convert.ToBase64String(encrypted);
		}

		public string EncryptToBase64Text(string sText, System.Text.Encoding encoding)
		{
			byte [] encrypted = m_provider.Encrypt(encoding.GetBytes(sText));
			return Convert.ToBase64String(encrypted);
		}




		public byte [] Decrypt(string sBase64Text)
		{
			byte [] encrypted = Convert.FromBase64String(sBase64Text);
			return m_provider.Decrypt(encrypted);
		}

		public byte [] Decrypt(byte [] data)
		{
			return m_provider.Decrypt(data);
		}


		public string DecryptToUtf8Text(string sBase64Text)
		{
            if (sBase64Text.Length == 0)
            {
                return string.Empty;
            }

			byte [] encrypted = Convert.FromBase64String(sBase64Text);
			byte [] decrypted =  m_provider.Decrypt(encrypted);
			return Encoding.UTF8.GetString(decrypted, 0, decrypted.Length);
		}

		public string DecryptUtf8Text(byte [] data)
		{
			byte [] decrypted = m_provider.Decrypt(data);
			return Encoding.UTF8.GetString(decrypted, 0, decrypted.Length);
		}


		public string DecryptToText(string sBase64Text, Encoding encoding)
		{
            if (sBase64Text.Length == 0)
            {
                return string.Empty;
            }

			byte [] encrypted = Convert.FromBase64String(sBase64Text);
			byte [] decrypted =  m_provider.Decrypt(encrypted);
			return encoding.GetString(decrypted, 0, decrypted.Length);
		}

		public string DecryptToText(byte [] data, Encoding encoding)
		{
			byte [] decrypted = m_provider.Decrypt(data);
			return encoding.GetString(decrypted, 0, decrypted.Length);
		}



	}
}
